﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using TCG.Engine.Core;

namespace TCG.Engine.Libraries
{
    public class FileCardLibrary : ICardLibrary
    {
        string file;
        public FileCardLibrary(string file)
        {
            this.file = file;
        }
        Dictionary<string, Card> cards;
        public Dictionary<string, Card> Cards
        { 
            get
            {
                if (cards == null)
                {
                    cards = LoadSet(file);
                }
                return cards;
            }
        }
        private static Dictionary<string, Card> LoadSet(string fileName)
        {
            using (var fs = File.OpenRead(fileName))
            {
                XmlSerializer s = new XmlSerializer(typeof(CardsInfo));
                var result = (CardsInfo)s.Deserialize(fs);
                return result.ToDictionary(ci => ci.Id, ci => new Card(ci));
            }
        }
    }
}
